package com.dattplace.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import com.dattplace.dao.IMongodbDAO;
import com.dattplace.dao.ISmallNoManagementDao;
import com.dattplace.model.Paginate;
import com.dattplace.model.SmallNo;
import com.dattplace.model.SmallNoLog;
import com.dattplace.model.SmallNoTask;
import com.dattplace.mongoSupport.QueryParamCreator;
import com.dattplace.util.StringUtil;



@Persistent
@Service("samllNoDao")
public class SmallNoManagementDao implements  ISmallNoManagementDao{

	@Autowired
	private IMongodbDAO dao;

	@Override
	public long queryMerchantCount(int currentPage, int pageSize,
			String smallNoName, String platformName) {
		
		List<Criteria> condList = new ArrayList<Criteria>();
		if (StringUtil.isNotEmptyString(smallNoName)) {
		    condList.add(QueryParamCreator.like("smallNoName", smallNoName));
		}
		
		if (StringUtil.isNotEmptyString(smallNoName)) {
		    condList.add(QueryParamCreator.like("platformName", platformName));
		}
		
		return dao.getRowCount(SmallNo.class, (Criteria[]) condList.toArray(new Criteria[0]));
	}

	@Override
	public List<SmallNo> querySmallNoList(int currentPage, int pageSize,
			String smallNoName, String platformName) {
		List<Order> orders = new ArrayList<Order>();
		Order order = new Order(Direction.DESC, "createDate");
		orders.add(order);
		
		
		List<Criteria> condList = new ArrayList<Criteria>();
		condList.add(QueryParamCreator.isNotNull("smallNoId"));
		
		if (StringUtil.isNotEmptyString(smallNoName)) {
		    condList.add(QueryParamCreator.like("smallNoName", smallNoName));
		}
		
		if (StringUtil.isNotEmptyString(smallNoName)) {
		    condList.add(QueryParamCreator.like("platformName", platformName));
		}
		
		return dao.getPOList(SmallNo.class,new Paginate(currentPage, pageSize), orders,(Criteria[]) condList.toArray(new Criteria[0]));
	}

	@Override
	public void saveSmallNo(SmallNo samllNo) {
		dao.saveOrUpdatePO(samllNo);
	}

	@Override
	public void saveSmallNoLog(SmallNoLog log) {
		dao.saveOrUpdatePO(log);
	}

	@Override
	public long queryTaskById(String merchantId, String smallNoId) {
		
		return dao.getRowCount(SmallNoTask.class, QueryParamCreator.eq("merchantId", merchantId),QueryParamCreator.eq("smallNoId", smallNoId));
	}
}
